package com.sven.provider;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MainController {

    @Value("${server.port}")
    String port;

    @Autowired
    HealthStatusService healthStatusService;

    @GetMapping("/getHi")
    public String getHi(){
        return "eureka-provider：Hi，我的端口："+ port;
    }

    @GetMapping("/health")
    public String health(@RequestParam("status") Boolean status) {
        healthStatusService.setStatus(status);
        return healthStatusService.getStatus();
    }

    @GetMapping("/getMap")
    public Map<String, String> getMap() {
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "500");
        return map;
    }

    @GetMapping("/getObj")
    public Person getObj() {
        Person person = new Person();
        person.setId(100);
        person.setName("xiaoming");
        return person;
    }

    @GetMapping("/getObjParam")
    public Person getObjParam(String name) {
        Person person = new Person(100,name);
        return person;
    }

    @PostMapping("/postLocation")
    public URI postLocation(@RequestBody Person person, HttpServletResponse response) throws Exception {
        URI uri = new URI("https://www.baidu.com/s?wd="+person.getName().trim());
        response.addHeader("Location", uri.toString());
        System.out.println("uri："+uri);
        return uri;
    }

    @PostMapping("/postParam")
    public Person postParam(@RequestBody Person person) {
        System.out.println("postParam入参:" + JSON.toJSONString(person));
        Person p = new Person();
        p.setId(100);
        p.setName("xiaoming-" + person.getName());
        return p;
    }

}
